<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019\3\7 0007
 * Time: 14:29
 */

namespace app\service;
use think\facade\Db;

class appletService
{
    //小程序积分商城数据库
    private $config=[
                'type'=>'mysql',
                'hostname'=>'127.0.0.1',
                'username'=>'root',
                'password'=>'zaozao2019@',
                'database'=>'we7',
            ];
    //小程序积分商城数据库
    private $config2=[
                'type'=>'mysql',
                'hostname'=>'127.0.0.1',
                'username'=>'root',
                'password'=>'zaozao2019@',
                'database'=>'zaozaomc',
            ];
    //小区列表
    public function communityList()
    {
        $list = Db::name('community')->order('id desc')->select();
        return $list;
    }
    //组团列表
    public function groupList($community_id)
    {
		$community=Db::name("community")->where("id",$community_id)->find();
		$list= Db::name('roominfo')->distinct(true)->field("zz_group")->where('community_id',$community["community_id"])->column("zz_group");
		/*if(empty($list[0])){
			$list=Db::name("community")->where("id",$community_id)->column("community_name");
		}*/
		return $list;
        
    }
    //幢数列表
    public function buildingList($group,$community_id)
    {
		$community=Db::name("community")->where("id",$community_id)->find();
        $list = Db::name('roominfo')->distinct(true)->field("building")->where('zz_group',$group)->where('community_id',$community["community_id"])->column('building');
		/*if($group==$community["community_name"]){
			$grp= Db::name('roominfo')->distinct(true)->field("group")->where('community_id',$community["community_id"])->column("group");
						
			//$list = Db::name('roominfo')->distinct(true)->field("building")->where(function($query){$query->where('group',$grp[0])->whereOr('group','')->whereOr('group',null)})->where('community_id',$community["community_id"])->column('building');
			$list = Db::name('roominfo')->distinct(true)->field("building")->where('group',$grp[0])->where('community_id',$community["community_id"])->column('building');
		}*/
        return $list;
    }
    //单元列表
    public function unitList($building,$group,$community_id)
    {
		$community=Db::name("community")->where("id",$community_id)->find();
        $list = Db::name('roominfo')->distinct(true)->field("unit")->where('community_id',$community["community_id"])->where('zz_group',$group)->where('building',$building)->column('unit');
        /*if($group==$community["community_name"]){
			$grp= Db::name('roominfo')->distinct(true)->field("group")->where('community_id',$community["community_id"])->column("group");
			$list = Db::name('roominfo')->distinct(true)->field("unit")->where('community_id',$community["community_id"])->where('group',$grp[0])->where('building',$building)->column('unit');
		}*/
		return $list;
    }
    //房屋列表
    public function roomList($unit,$building,$group,$community_id)
    {
		$community=Db::name("community")->where("id",$community_id)->find();
        $list = Db::name('roominfo')->distinct(true)->field("room")->where('community_id',$community["community_id"])->where('zz_group',$group)->where('unit',$unit)->where('building',$building)->column('room');
        /*if($group==$community["community_name"]){
			$grp= Db::name('roominfo')->distinct(true)->field("group")->where('community_id',$community["community_id"])->column("group");
			$list = Db::name('roominfo')->distinct(true)->field("room")->where('community_id',$community["community_id"])->where('group',$grp[0])->where('unit',$unit)->where('building',$building)->column('room');
		}*/
		return $list;
    }
    //模糊查询小区
    public function communityName($communityName)
    {
        $list = Db::name('community')->where('community_name',"like","%$communityName%")->select();
        return $list;
    }
	 
    
	 //小区列表
    public function communityNotice($id)
    {
        $list = Db::name('community')->where('id',$id)->value('name');
        return $list;
    }
	
//------------------------------------------------//
    //查询完整门牌号
    public function roomAddress($community_id,$group,$building,$unit,$room){
        $community=Db::name("community")->where("id",$community_id)->find();
        $list=Db::name('roominfo')->where('community_id',$community["community_id"])->where('zz_group',$group)->where('building',$building)->where('unit',$unit)->where('room',$room)->find();
		
		return $list;
	}
	//查询种类
    public function cost_type($out_room_id){
        $cost_type = Db::name('bill')->distinct(true)->field('cost_type')->where('out_room_id',$out_room_id)->where('status','WAIT_PAYMENT')->column('cost_type');
        return $cost_type;
    }
    //根据种类查账单
    public function cost_type_bill($out_room_id,$b){
        $cost_type_bill = Db::name('bill')->where('out_room_id',$out_room_id)->where('status','WAIT_PAYMENT')->where('cost_type',$b)->order('id desc')->select();
        
        return $cost_type_bill ;
    }
	//根据种类查账单
    public function select($out_room_id,$b){
        $cost_type = Db::name('bill')->where('out_room_id',$out_room_id)->where('status','WAIT_PAYMENT')->where('cost_type',$b)->select();
        return $cost_type ;
    }
	
	////根据房号查询账单
	public function billRoomIdList($out_room_id){
        return Db::name('bill')->where('out_room_id',$out_room_id)->where('status','WAIT_PAYMENT')->select();
    }

    //
    public function billsum($out_room_id){
        return Db::name('bill')->where('out_room_id',$out_room_id)->where('status','WAIT_PAYMENT')->sum('bill_entry_amount');
    }


    //查询打印机终端
    public function communityMachine($community_id){
        return Db::name('community')->where('community_id',$community_id)->find();
    }

    //修改指定账单数据
    public function billEdit($data){
        return Db::name('bill')->update($data);
    }
    //查询账单
    public function tradeNoInfo($trade_no){
        return Db::name('bill')->where('out_trade_no',$trade_no)->select();
    }
    //删除支付宝账单
    public function alibilldel($id){
        $info = Db::table("bill")->find($id);
        $token = (new billService())->billinfoToken($info['community_id']);
        $api = new apiService(config('app')['roombilldel'], 'https://openapi.alipay.com/gateway.do');
        $res = $api->roomBillDel(json_encode(['community_id' =>  $info['community_id'], 'bill_entry_id' => $info['bill_entry_id'], 'token' => $token]));
        return $res;
    }
    //小程序积分商城绑定房屋
    public function updateUserRoom($roominfo,$uid){
        $data["out_room_id"]=$roominfo["out_room_id"];
        $data["room_address"]=$roominfo["address"];
        $data["community_id"]=$roominfo["community_id"];
        $data["id"]=$uid;
        $communityinfo=Db::table("community")->where("community_id",$roominfo["community_id"])->find();
        $data["community_name"]=$communityinfo["community_name"];
        return Db::connect($this->config)->table("ims_zofui_sales_user")->update($data);
    }

    //小程序早早买菜绑定房屋
    public function mcUpdateUserRoom($roominfo,$uid){
        $data["out_room_id"]=$roominfo["out_room_id"];
        $data["room_address"]=$roominfo["address"];
        $data["community_id"]=$roominfo["community_id"];
        $data["id"]=$uid;
        $communityinfo=Db::table("community")->where("community_id",$roominfo["community_id"])->find();
        $data["community_name"]=$communityinfo["community_name"];
        return Db::connect($this->config2)->table("hjmallind_user")->update($data);
    }
}
